/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.account;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.ui.tree.DropTree;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.service.account.SysUserService;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-11-27
 */
@Controller
@RequestMapping("/sysuser")
public class SysUserController {

    private SysUserService sysuserService;

    /****************************************
     * Insert a instace of SysUser
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertSysUser(@RequestBody SysUser sysuser) throws Exception {
        sysuser.setStatmentId("SysUser.insert");
        String status = "failure";
        sysuserService.insert(sysuser);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of SysUser
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody SysUser sysuser) throws Exception {
        sysuser.setStatmentId("SysUser.update");
        String status = "failure";
        sysuserService.update(sysuser);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of SysUser
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody SysUser sysuser) throws Exception {
        sysuser.setStatmentId("SysUser.delete");
        String status = "failure";
        sysuserService.batchDelete(sysuser);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of SysUser
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        SysUser sysuser = new SysUser();
        sysuser.setStatmentId("SysUser.queryAsList");
        BeanUtils.populate(sysuser, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(sysuser, pageMap);
        List<BaseModel> sysuserList = sysuserService.queryAsList(sysuser);
        Map<String, Object> renderMap = GridUIMapper.render(sysuser, sysuserList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of SysUser
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(SysUser sysuser) throws Exception {
        return sysuserService.queryAsObject(sysuser);
    }

    @RequestMapping("/queryUserTree")
    @ResponseBody
    public Object queryUserTree(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        SysUser sysuser = new SysUser();
        sysuser.setStatmentId("SysUser.queryUserAsTree");
        BeanUtils.populate(sysuser, paramMap);
        return sysuserService.queryUserAsTree(sysuser);
    }

    @RequestMapping("/userMgr")
    public String userMgr() {
        return "sys/sysuserList";
    }

    @RequestMapping("/dropTree")
    public String userDropTree(DropTree dropTree, ModelMap map) {
        map.put("dropTree", dropTree);
        return "sys/userDropTree";
    }

    @Resource
    public void setSysUserService(SysUserService sysuserService) {
        this.sysuserService = sysuserService;
    }
}
